package dev.guide.net;

import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

import android.util.Log;

public class DownloadThread extends Thread {
	private static final String TAG = "DownloadThread";
	private RandomAccessFile saveFile;
	private URL downUrl;
	private int block;

	/* 下载开始位置 */private int threadId = -1;
	private int startPos;
	private int downLength;
	private boolean finish = false;
	private FileDownloader downloader;

	public DownloadThread(FileDownloader downloader, URL downUrl,
			RandomAccessFile saveFile, int block, int startPos, int threadId) {

		this.downUrl = downUrl;
		this.saveFile = saveFile;
		this.block = block;
		this.startPos = startPos;
		this.downloader = downloader;
		this.threadId = threadId;
		this.downLength = startPos - (block * (threadId - 1));
	}

	public void run() {
		if (downLength < block) {// 未下载完成

			try {

				HttpURLConnection http = (HttpURLConnection) downUrl
						.openConnection();
				http.setRequestMethod("GET");
				http
						.setRequestProperty(
								"Accept",
								"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x- shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword,*/*");
				http.setRequestProperty("Accept-Language", "zh-CN");
				http.setRequestProperty("Referer", downUrl.toString());
				http.setRequestProperty("Charset", "UTF-8");
				http
						.setRequestProperty("Range", "bytes=" + this.startPos
								+ "-");
				http
						.setRequestProperty(
								"User-Agent",
								"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR3.0.4506.2152; .NET CLR 3.5.30729)");
				http.setRequestProperty("Connection", "Keep-Alive");
				InputStream inStream = http.getInputStream();
				int max = block > 1024 ? 1024 : (block > 10 ? 10 : 1);
				byte[] buffer = new byte[max];
				int offset = 0;
				print("线程 " + this.threadId + "从位置" + this.startPos + "开始下载 ");
				while (downLength < block
						&& (offset = inStream.read(buffer, 0, max)) != -1) {

					saveFile.write(buffer, 0, offset);
					downLength += offset;
					downloader.update(this.threadId, block * (threadId - 1)
							+ downLength);
					downloader.saveLogFile();
					downloader.append(offset);
					int spare = block - downLength;// 求剩下的字节数
					if (spare < max)
						max = (int) spare;
				}
				saveFile.close();
				inStream.close();
				print("线程 " + this.threadId + "完成下载 ");
				this.finish = true;
				this.interrupt();
			} catch (Exception e) {
				this.downLength = -1;
				print("线程" + this.threadId + ":" + e);
			}
		}
	}

	private static void print(String msg) {
		Log.i(TAG, msg);
	}

	/**
	 * 下载是否完成
	 * 
	 * @return
	 */

	public boolean isFinish() {
		return finish;
	}

	/**
	 * 已经下载的内容大小
	 * 
	 * @return 如果返回值为-1,代表下载失败
	 */

	public long getDownLength() {
		return downLength;
	}

}
